package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import form.ConnectionDB;
import model.Muctintuc;
import model.Tintuc;

public class TintucDAO {

	public static boolean insertYesEvent(Tintuc tintuc) throws SQLException,
			ClassNotFoundException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into tintuc(hinh,title,title_khongdau,muctintuc_id,ngayviet,thoigianviet,sukien_id,noidung) values (?,?,?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, tintuc.getHinh());
		ps.setString(2, tintuc.getTitle());
		ps.setString(3, tintuc.getTitle_khongdau());
		ps.setInt(4, tintuc.getMuctintuc().getIdmuctintuc());
		ps.setDate(5, tintuc.getNgayviet());
		ps.setTime(6, tintuc.getThoigianviet());
		ps.setString(7, tintuc.getSukienid());
		ps.setString(8, tintuc.getNoidung());

		int change = 0;
		change = ps.executeUpdate();
		connection.close();

		if (change > 0) {
			return true;
		} else {

			return false;
		}

	}

	public static boolean insertNoEvent(Tintuc tintuc) throws SQLException,
			ClassNotFoundException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into tintuc(hinh,title,title_khongdau,muctintuc_id,ngayviet,thoigianviet,noidung) values (?,?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, tintuc.getHinh());
		ps.setString(2, tintuc.getTitle());
		ps.setString(3, tintuc.getTitle_khongdau());
		ps.setInt(4, tintuc.getMuctintuc().getIdmuctintuc());
		ps.setDate(5, tintuc.getNgayviet());
		ps.setTime(6, tintuc.getThoigianviet());
		ps.setString(7, tintuc.getNoidung());

		int change = 0;
		change = ps.executeUpdate();
		connection.close();

		if (change > 0) {
			return true;
		} else {

			return false;
		}

	}

	public static ArrayList<Tintuc> select() throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		ArrayList<Tintuc> listTintuc = new ArrayList<>();
		String sql = "select * from tintuc";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet resultSet = ps.executeQuery();
		while (resultSet.next()) {
			int tintuc_id = resultSet.getInt("tintuc_id");
			String hinh = resultSet.getString("hinh");
			String title = resultSet.getString("title");
			String title_khongdau= resultSet.getString("titlekhongdau");
			int muctintuc_id = resultSet.getInt("muctintuc_id");
			Date ngayviet = resultSet.getDate("ngayviet");
			Time thoigianviet = resultSet.getTime("thoigianviet");
			String sukien_id = resultSet.getString("sukien_id");
			String noidung = resultSet.getString("noidung");
			listTintuc.add(new Tintuc(tintuc_id, hinh, title, title_khongdau,new Muctintuc(muctintuc_id) , ngayviet, thoigianviet,sukien_id, noidung));
		}
		connection.close();

		return listTintuc;

	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException, java.text.ParseException {
//		ArrayList<Tintuc> listTintuc = select();
//		for (Tintuc mucTintuc : listTintuc) {
//			System.out.println(mucTintuc.toString());
//		}
		
	}
	
	

}
